package com.example.kick.java8;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class UsualPractise {

    public static void main(String[] args) {
        List<String> l1 = Arrays.asList("1", "2", null);
        List<String> l2 = Arrays.asList("4", "5", "6");
        List<String> l3 = Arrays.asList("7", "8", "9");
        List<List<String>> query = performWarehouseQueries(Arrays.asList(l1, l2, l3));

        findTotalPower(Arrays.asList(2, 3, 2, 1));
    }

    public static int minBy(List<Integer> numbers) {
        return numbers.stream().min(Comparator.naturalOrder()).get();
    }

    public static int sumBy(List<Integer> numbers) {
        return numbers.stream().collect(Collectors.summingInt(Integer::valueOf));
    }

    /**
     * 给定一个整数集合,按顺序对其中的每个整数,求其与集合中从自身开始往后的所有整数的组合的最小值与和的乘积之和
     * 例如集合[2, 3, 2, 1],则power[0]=min[2]*sum[2]=2*2=4; power[0,1]=min[2,3]*sum[2,3]=2*5=10;
     * power[0,2]=min[2,3,2]*sum[2,3,2]=2*7=14; power[0,3]=min[2,3,2,1]*sum[2,3,2,1]=1*8=8;
     * <p>
     * power[1]=min[3]*sum[3]=3*3=9; power[1,2]=min[3,2]*sum[3,2]=2*5=10; power[1,3]=min[3,2,1]*sum[3,2,1]=1*6=6;
     * <p>
     * power[2]=min[2]*sum[2]=2*2=4; power[2,3]=min[2,1]*sum[2,1]=1*3=3;
     * <p>
     * power[3]=min[1]*sum[1]=1*1=1;
     * 结果=36 + 9 + 10 + 6 + 4 + 3 + 1=69
     *
     * @param power
     * @return
     */
    public static int findTotalPower(List<Integer> power) {
        int total = 0;
        for (int i = 0; i < power.size(); i++) {
            for (int j = power.size() - 1; j >= i; j--) {
                List<Integer> temp = power.subList(i, power.size() - j + i);
                int min = minBy(temp);
                int sum = sumBy(temp);
                total += min * sum;
            }
            System.out.println("i:" + i + ",total:" + total);
        }
        System.out.println("final total:" + total);
        return total;
    }

    /**
     * 将一个集合中的集合变成子集合长度是3的集合
     *
     * @param query
     * @return
     */
    public static List<List<String>> performWarehouseQueries(List<List<String>> query) {
        List<List<String>> resultList = new ArrayList<>();
        String word = null;
        List<String> tempList = null;
        for (List<String> q : query) {
            for (int i = 0; i < q.size(); i++) {
                word = q.get(i);
                if (StringUtils.isEmpty(word)) {
                    continue;
                }
                if (null == tempList) {
                    tempList = new ArrayList<>();
                } else if (tempList != null && tempList.size() == 3) {
                    tempList = new ArrayList<>();
                    resultList.add(tempList);
                }
                if (tempList != null) {
                    tempList.add(word);
                }
            }
        }
        return resultList;
    }

    /**
     * 给定一个整数,从1到该整数的范围内:
     *    1.若既能被3也能被5整除,则打印出"FizzBuzz"
     *    2.若只能被3整除,则打印出"Fizz"
     *    3.若只能被5整除,则打印出"Buzz"
     *    4.其余数打印本身
     * @param n
     */
    public static void fizzBuzz(int n) {
        // Write your code here
        IntStream.rangeClosed(1, n).forEach(k -> {
            if (k % 15 == 0) {
                System.out.println("FizzBuzz");
            } else if (k % 3 == 0) {
                System.out.println("Fizz");
            } else if (k % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(k);
            }
        });
    }

}
